<?php

namespace App\Admin\Controllers;
use Faker\Factory;
use App\Services\SwitchService;
use Slowlyo\OwlAdmin\Controllers\AdminController;

/**
 * 交换机管理
 *
 * @property SwitchService $service
 */
class SwitchController extends AdminController
{
	protected string $serviceName = SwitchService::class;

	public function list()
	{
		$crud = amis()->CRUDCards()
			->api('/switch?_action=getData')
			->filterTogglable()
			->filterDefaultVisible(false) // 隐藏筛选栏
			->defaultParams([
				'perPage' => 1000,
				'page' => 1,
			])
			->filter(
				$this->baseFilter()->body([
					amis()->TextControl('name', '交换机名称')->size('md')->clearable(true)
				])
			)
			->headerToolbar([
				$this->createButton('dialog'),
				...$this->baseHeaderToolBar()
			])
			->card(
				amis()->Card()
				->header([
					'title' => '${name}',
					'subTitle' => '${switch_model}-${ip}',
					'avatar'=>'/admin-assets/switch.png',
					"avatarClassName"=>"pull-left thumb-lg m-r m-t",
				])
				->toolbar([
					amis()->Tpl()->badge()->className('label label-success')->text("正常"),
				])
				->body(
					amis()->Grid()->className('mt-2')->columns([ //先分两列
						amis()->GridColumn()->md(6)->body(
							amis()->Flex()->items([ 
								amis()->Progress()->value('${cpu}')->animate(true)->valueTpl('CPU:${value}%'),
								amis()->StaticExactControl()->value('${remark}'),
							])->direction('column'), // 再垂直分布
						),
						amis()->GridColumn()->md(6)->body(
							amis()->Flex()->items([ 
								amis()->Progress()->value('${memory}')->animate(true)->valueTpl('RAM:${value}%'),
							])->direction('column'), // 再分两行
						),
					]),
				)
				->actions([
					$this->port('dialog'),
					$this->rowEditButton('dialog'),
					$this->rowDeleteButton()
				])
			);
		return $this->basePage()->body($crud);

	}

	public function form($isEdit = false)
	{
		return $this->baseForm()->body([
			amis()->TextControl('name', '交换机名称'),
			amis()->TextControl('ip', '交换机IP'),
			amis()->SwitchControl('gather', '是否采集流量')->onText('启用')->offText('禁用')->defaultValue(1),
			amis()->SwitchControl('up_info', '抓取端口信息')->onText('启用')->offText('禁用')->defaultValue(1)->description('第一次添加或更新端口时开启'),
			amis()->TextControl('snmp_read', 'SNMP读团体名'),
			amis()->TextControl('snmp_write', 'SNMP写团体名'),
			amis()->TextareaControl('remark', '备注'),
			amis()->SwitchControl('snmp_status', '状态')->onText('启用')->offText('禁用')->defaultValue(1),

		]);
	}
	// 查看端口
	public function port()
	{
		 $action = amis()->LinkAction()->link('/switch_port?switchId=${id}')->label('查看端口');
		 return $action;
	}

}
